(function(){

'use strict';

angular.module('controllers.faultLocation.pbossOm',[])
  .controller('businessDetailController', businessDetailController);

function businessDetailController ($scope, pbossOmServer,$filter,COREDATA,CoreService,CoreLoading) {
	$scope.flag = 1;
	$scope.shoumun = 1;
	$scope.errorInfo = COREDATA.ERRORINFO; //日期错误信息
  //查询条件参数
	$scope.businessTypes = [
		{businessCode:'01',businessName:'号码新同步'},
		{businessCode:'02',businessName:'用户状态同步'},
		{businessCode:'03',businessName:'EC信息同步EC信息同步'},
		{businessCode:'04',businessName:'EC企业产品订购信息同步'},
		{businessCode:'05',businessName:'EC成员产品订购关系信息同步'}
	];
	$scope.translate = {
		businessCode: [],
		startTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 31),
		endTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1)
	};

		var paramsData = {};
	$scope.queryBusiness = function(){
		$scope.shoumun==1?$scope.shoumun=0:$scope.shoumun=1;
		$scope.startTime = $filter('date')($scope.translate.startTime,'yyyyMMdd');
		$scope.endTime = $filter('date')($scope.translate.endTime,'yyyyMMdd');
		paramsData.startTime = $filter('date')($scope.translate.startTime,'yyyy-MM-dd');
		paramsData.endTime = $filter('date')($scope.translate.endTime,'yyyy-MM-dd');
		
		if($scope.shoumun === 0){
			CoreLoading.setState(true);
			pbossOmServer.queryPbossBusiness(paramsData,queryBusiness);
		}
	};

	$scope.resetValue = function(){
		$scope.translate = {
			businessCode: [],
			startTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 31),
			endTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1)
		};
	};

	var businessEchart = echarts.init(document.getElementById('pbossRate'));
	var businessNum = echarts.init(document.getElementById('pbossNum'));

  function queryBusiness(res) {
  	  CoreLoading.setState(false);
	  if(res.state === '10000' && res.data.pageData.length > 0 ){
		  $scope.flag = true;
		  $scope.flagObj = true;
		  $scope.businessData = res.data.pageData;

		  var nameArr = [];
		  var valueRate = [];
		  var valueNum = [];
		  angular.forEach($scope.businessData,function(itemData){
			  valueRate.push(itemData.successRate);
			  valueNum.push(itemData.businessCount);
			  nameArr.push(itemData.businessName);
		  });

		  var itemStyle =  {
			  normal: { // 系列级个性化，横向渐变填充
				  borderRadius: 5,
				  label: {
					  show: true,
					  textStyle: {
						  fontSize: '10',
						  fontFamily: '微软雅黑',
						  fontWeight: 'bold'
					  }
				  }
			  }
		  };
		  var option = {
			  title: {
				  text: 'PBOSS各业务成功率'+$scope.startTime+'-'+$scope.endTime,
				  x: 'center'
			  },
			  tooltip: {
				  trigger: 'axis',
				  axisPointer: {
					  type: 'shadow'
				  }
			  },
			  legend: {
				  orient: 'vertical',
				  x: 'right',
				  y: 'center',
				  data: ['成功率']
			  },
			  // calculable: true,
			  grid: {
				  x2: '13%'
			  },
			  xAxis: [
				  {
					  type: 'category',
					  data: nameArr
				  }
			  ],
			  yAxis: [
				  {
					  type: 'value',
					  min:80,
					  max:100,
					  axisLabel : {
						  formatter: '{value} %'
					  }
				  }
			  ],
			  series: [
				  {
					  name: '成功率',
					  type: 'bar',
					  itemStyle: itemStyle,
					  data: valueRate
				  }
			  ]
		  };

		  businessEchart.setOption(option);

		  var optionNum = {
			  title: {
				  text: 'PBOSS各业务交易笔数'+$scope.startTime+'-'+$scope.endTime,
				  x: 'center'
			  },
			  tooltip: {
				  trigger: 'axis',
				  axisPointer: {
					  type: 'shadow'
				  }
			  },
			  legend: {
				  orient: 'vertical',
				  x: 'right',
				  y: 'center',
				  data: ['交易笔数']
			  },
			  // calculable: true,
			  grid: {
				  x2: '13%'
			  },
			  xAxis: [
				  {
					  type: 'category',
					  data: nameArr
				  }
			  ],
			  yAxis: [
				  {
					  name:'(笔数)',
					  type: 'value',
					 
				  }
			  ],
			  series: [
				  {
					  name: '交易笔数',
					  type: 'bar',
					  itemStyle: itemStyle,
					  data: valueNum
				  }
			  ]
		  };
		  businessNum.setOption(optionNum);
		  exportExcel();
	  }else{
	  	CoreService.dialog(res.state);
	  }
  }

  function exportExcel(){
  	$scope.outData = {
  		startTime: $scope.startTime,
  		endTime: $scope.endTime,
  		title: $scope.startTime+'至'+$scope.endTime+'业务详情统计'
  	};
  	$scope.exportUrl = COREDATA.FORMPREFIX + 'OMdisplay/PBossOM/businessReportAnalysis/businessDetail/export'; //表导出的请求地址

  }
  // $scope.queryBusiness();
}

})();